package com.ningxia.water.controller.geographic;

import com.ningxia.water.entity.ZtAreaInfo;
import com.ningxia.water.entity.ZtCityInfo;
import com.ningxia.water.entity.ZtDistrictInfo;
import com.ningxia.water.reuslt.Result;
import com.ningxia.water.reuslt.ResultResponse;
import com.ningxia.water.service.IZtCityInfoService;
import com.ningxia.water.utils.CommonUtils;
import com.ningxia.water.vo.out.ZoneVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huangzhen
 * @create 2021-05-14 21:02
 */
@RestController
@RequestMapping("/city")
@Api(tags = {"地理信息-1-城市的API"})
public class CityController {

    @Autowired
    private IZtCityInfoService cityInfoService;

    @GetMapping("/getcityinfo")
    @ApiOperation("获取全部城市地理信息")
    public Result getCityInfo() {
        List<ZoneVo> list = cityInfoService.getCityInfos();
        return ResultResponse.getSuccessResult(list);
    }

    @PostMapping("/insertcityinfo")
    @ApiOperation(value = "新增城市")
    public Result insertCityInfo(@RequestBody ZtCityInfo cityInfo) {
        return CommonUtils.commonResult(cityInfoService.save(cityInfo));
    }

    @PutMapping("/updatecityinfo")
    @ApiOperation(value = "根据ID更新城市")
    public Result updateCityInfo(@RequestBody ZtCityInfo cityInfo) {
        return CommonUtils.commonResult(cityInfoService.updateById(cityInfo));
    }


    @DeleteMapping("/deletecityinfo")
    @ApiOperation(value = "根据id删除城市")
    public Result deleteCityInfo(@RequestParam("cityId") Integer cityId) {
        Integer count = cityInfoService.findByCityId(cityId);
        if (count == 0) {
            return CommonUtils.commonResult(cityInfoService.removeById(cityId));
        }
        return ResultResponse.getFailResult("该市下含有区县或工业园区");

    }

    @GetMapping("/getalldistrict")
    @ApiOperation(value = "获取该城市下的所有区县")
    public Result getAllDistrict(@RequestParam("cityId") Integer cityId){
        List<ZtDistrictInfo> list = cityInfoService.getAllDistrict(cityId);
        return ResultResponse.getSuccessResult(list);
    }

    @GetMapping("/getallarea")
    @ApiOperation(value = "获取该城市下的所有工业园区")
    public Result getAllArea(@RequestParam("cityId") Integer cityId){
        List<ZtAreaInfo> list = cityInfoService.getAllArea(cityId);
        return ResultResponse.getSuccessResult(list);
    }

}
